# from turtledemo.chaos import g
from flask import Flask, session, g,render_template
import config
from exts import db, mail
from blueprints.auth import bp as auth_bp
from blueprints.qa import bp as qa_bp
from flask_migrate import Migrate
from models import UserModel

app = Flask(__name__)
app.debug = True  # 可选：显式设置 debug 模式
# 绑定配置文件
app.config.from_object(config)

# 先创建在绑定
db.init_app(app)
# 邮箱配置
mail.init_app(app)
# 注册蓝图
app.register_blueprint(auth_bp)
app.register_blueprint(qa_bp)
# 初始化数据库迁移（映射数据库）
migrate = Migrate(app, db)


@app.before_request
def my_before_request():
    user_id = session.get('user_id')
    if user_id:
        user = UserModel.query.get(user_id)
        setattr(g, 'user', user)
    else:
        setattr(g, 'user', None)


@app.context_processor
def my_context_processor():
    return {'user': g.user}

if __name__ == '__main__':
    # app.run(debug=True,host='0.0.0.0',port=8000)  # 开启Debug模式、修改地址host、访问端口号port
    app.run()
